package com.shengqi.xinnongren.work.ui.home.activities;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.shengqi.xinnongren.R;
import com.shengqi.xinnongren.common.base.BaseMvpActivity;
import com.shengqi.xinnongren.common.utils.ImageUtils;
import com.shengqi.xinnongren.common.utils.ToastHelper;
import com.shengqi.xinnongren.work.model.HttpRespond;
import com.shengqi.xinnongren.work.ui.home.presenter.StoreDetailsPresenter;
import com.shengqi.xinnongren.work.ui.home.view.StoreDetailsView;
import com.shengqi.xinnongren.work.ui.user.model.StoreDetailBean;

import butterknife.BindView;
import butterknife.OnClick;

public class StoreDetailsActivity extends BaseMvpActivity<StoreDetailsView, StoreDetailsPresenter> implements StoreDetailsView {


    @BindView(R.id.iv_cover)
    ImageView mIvCover;
    @BindView(R.id.tv_store_name)
    TextView mTvStoreName;
    @BindView(R.id.tv_distance)
    TextView mTvDistance;
    @BindView(R.id.tv_open_time)
    TextView mTvOpenTime;
    @BindView(R.id.tv_address)
    TextView mTvAddress;
    @BindView(R.id.tv_service_call)
    TextView mTvServiceCall;
    @BindView(R.id.tv_tips)
    TextView mTvTips;

    private String storeId; //门店ID
    private String longt;
    private String lat;

    @Override
    protected int setContentLayoutRes() {
        return R.layout.activity_store_details;
    }

    @Override
    protected void initView() {

        storeId = getIntent().getStringExtra("id");
        longt = getIntent().getStringExtra("x");
        lat = getIntent().getStringExtra("y");

    }

    @Override
    protected void initData() {

        mPresenter.getStoreDetail(storeId,longt,lat);
    }

    //打开地图
    @OnClick(R.id.ll_open_map)
    void ll_open_map() {
        PackageManager manager = getApplicationContext().getPackageManager();
        Uri uri = Uri.parse("geo:" + curLat + "," + curLong);
        Intent it = new Intent(Intent.ACTION_VIEW, uri);
        if (!manager.queryIntentActivities(it, PackageManager.MATCH_DEFAULT_ONLY).isEmpty()) {
            startActivity(it);
            return;
        }
        Toast.makeText(this, "抱歉，请先安装地图应用", Toast.LENGTH_SHORT).show();


    }

    //打电话
    @OnClick(R.id.ll_call_phone)
    void ll_call_phone() {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:" + mTvServiceCall.getText().toString()));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    @Override
    protected StoreDetailsPresenter createPresenter() {
        return new StoreDetailsPresenter();
    }

    @Override
    public void showLoadingView() {

    }

    @Override
    public void hideLoadingView() {

    }

    @Override
    public void onNetworkError() {

    }

    public static void start(Context context, String id,String lat,String longt) {
        Intent intent = new Intent(context, StoreDetailsActivity.class);
        intent.putExtra("id", id);
        intent.putExtra("y", lat);
        intent.putExtra("x", longt);
        context.startActivity(intent);
    }

    private double curLat;
    private double curLong;

    @Override
    public void showStoreDetail(HttpRespond<StoreDetailBean> response) {
        if (response.result == 1) {

            StoreDetailBean detailBean = response.data;
            curLong = detailBean.getX();
            curLat = detailBean.getY();

            ImageUtils.showImage(this, detailBean.getInteriorPic(), mIvCover);
            mTvStoreName.setText(detailBean.getName());
            mTvDistance.setText("距离您" + detailBean.getDistance() + "千米");
            mTvOpenTime.setText(detailBean.getBusinessHours1());
            mTvAddress.setText(detailBean.getAddress());
            mTvServiceCall.setText(detailBean.getPhone());
            mTvTips.setText(detailBean.getTips());

        } else
            ToastHelper.getInstance(this).showWarn(response.message);

    }
}
